// 获取ipcRenderer
export const getIpcRenderer = () => {
    let ipcRenderer;
    try {
        ipcRenderer = window.require('electron')?.ipcRenderer;
    } catch (e) {
        console.log("ipcRenderer异常", e);
    }
    return ipcRenderer;
}

// 单位换算kb转mb
export const kbToMb = size => (size && size.toString().length >= 1) ?
    Math.floor(parseInt(size) * 100 / 1024 / 1024) / 100 + "M" : "";

// 数据转换
export const bufferToBlob = dataBuffer => dataBuffer ?
    window.URL.createObjectURL(new Blob([dataBuffer])) : "";

// buffer.toString('base64')
export const bufferToBase64 = dataBuffer => dataBuffer ?
    'data:image/jpg;base64,' + dataBuffer : "";

// 时间转换工具
export const secondToMinute = function (time) {
    let second = time % 60
    if (time < 60) {
        return '00.' + second.toString().padStart(2, '0')
    } else {
        let minute = Math.floor(time / 60)
        return minute.toString().padStart(2, '0') + '.' + second.toString().padStart(2, '0')
    }
}

// 获取本地会话数据，并返回json格式数据
export const getLocalStorageJson = (key, type = "List") => {
    if ("String" === type) {
        return localStorage.getItem(key);
    } else {
        let dataListJson = [];
        let dataList = localStorage.getItem(key);
        if (dataList) {
            dataListJson = JSON.parse(dataList);
        }
        return dataListJson;
    }
}

export const initLazyIntersectionObserver = function (fn) {
    return new IntersectionObserver(
        entrys => entrys.forEach(entry => fn(entry)),
        {
            rootMargin: '0px',
            threshold: 0,
        }
    );
}
